//
//  DiaryDetailView.swift
//  MyDiary2025
//
//  Created by rolodestar on 2025/6/26.
//

import SwiftUI
import MapKit

struct DiaryDetailView: View {
    @State var showMapFullScreen: Bool = false
    var diary: DiaryModel
    
    var body: some View {
        // 使用GeometryReader获取父容器尺寸
        GeometryReader { geometry in
            
               
                
                // ScrollView应该占据剩余的所有垂直空间
                ScrollView {
                    // 使用VStack包裹所有内容，确保正确计算高度
                    VStack(alignment: .leading, spacing: 20) {
                        ZStack(alignment:.center){
                            RoundedRectangle(cornerRadius: 12)
                                .foregroundStyle(.body.opacity(0.4))
                                .offset(x: 5, y: 5)
                                .shadow(radius: 5)
                                
                            DiaryMapView(diary: diary)
                                .frame(width: geometry.size.width - 30, height: geometry.size.width * 0.618 > geometry.size.height / 3 ? geometry.size.height / 3 : geometry.size.width * 0.618)
                                .clipShape(RoundedRectangle(cornerRadius: 17))
                                .onTapGesture {
                                    showMapFullScreen.toggle()
                                }
                                .fullScreenCover(isPresented: $showMapFullScreen) {
                                    DiaryMapView(diary: diary)
                                        .onTapGesture {
                                            showMapFullScreen = false
                                        }
                                }
                        }
                        HStack {
                            Text(diary.title)
                                .font(.title)
                                .foregroundStyle(Color("title"))
                                
                           
                        }
                        ZStack(alignment: .leading) {
                            RoundedRectangle(cornerRadius: 12)
                                .foregroundStyle(.back.opacity(1))
                                .shadow(radius: 3)
                            HStack {
                                Text(diary.content)
                                    .font(.body)
                                    .foregroundStyle(Color("body"))
                                Spacer()
                            }.padding()
                        }
                        DiaryDetailTagView(diary: diary)
                        DiaryDetailMediaView(diary: diary)
                            .frame(height: CGFloat(diary.images.count) / 3 * (geometry.size.width - 30) / 3)
                            .padding(.bottom, 20)
                        ZStack(alignment: .leading) {
                            RoundedRectangle(cornerRadius: 12)
                                .foregroundStyle(.background.opacity(1))
                                .shadow(radius: 0.5)
                            HStack {
                                Text(diary.createDate.viewDate)
                                    .foregroundStyle(Color("warning"))
                                Text(diary.weather.image)
                                    .foregroundStyle(Color("warning"))
                                Text(diary.weather.chineseDescription)
                                    .foregroundStyle(Color("warning"))
//                                Spacer()
                            }
                            .font(.footnote)
                            .foregroundStyle(diary.weather.color)
                            .padding()
                                 
                        }
                       
                        
                        
                        Divider()
                            .frame(height: 3)
                        DiaryDetailCommentView(diary: diary)
                       
                    }
                    .padding()
                    .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
                    // 保持原有内边距
                }
//                .frame(height: geometry.size.height - (geometry.size.width * 0.618)) // 计算ScrollView高度
            }
            .background(Color("back"))
        
    }
}

#Preview {
    DiaryDetailView(diary: DiaryModel.debugData)
}
